package com.open.rbac.model.entity.id;

import org.springframework.lang.NonNull;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

import com.open.center.jpa.model.entity.IdEntity;
import com.open.rbac.constant.GenderType;
import com.open.rbac.constant.IdType;
import com.open.rbac.constant.AdminConstant;

/**
 * Id信息
 *
 * @author Riche's
 * @since 2023/4/9
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@DynamicUpdate
@SQLRestriction("is_deleted = false")
@SQLDelete(sql = "UPDATE identity SET is_deleted = 1 WHERE id = ?")
@Entity(name = "identity")
public class Identity extends IdEntity {

    @ColumnTransformer(
            read = "AES_DECRYPT(UNHEX(name), " + AdminConstant.MYSQL_AES_KEY + ")",
            write = "HEX(AES_ENCRYPT(?, " + AdminConstant.MYSQL_AES_KEY + "))"
    )
    @Size(max = 128)
    @NonNull
    @Column(name = "name", nullable = false, length = 128)
    private String name;

    @Convert(converter = GenderType.Converter.class)
    @Column(name = "gender", nullable = false, length = 32)
    private GenderType gender;

    @Column(name = "ethnicity", length = 64)
    private String ethnicity;

    @Convert(converter = IdType.Converter.class)
    @Column(name = "id_type", nullable = false, length = 64)
    private IdType idType;

    @Column(name = "id_number", nullable = false, length = 128)
    @ColumnTransformer(
            read = "AES_DECRYPT(UNHEX(id_number), " + AdminConstant.MYSQL_AES_KEY + ")",
            write = "HEX(AES_ENCRYPT(?, " + AdminConstant.MYSQL_AES_KEY + "))"
    )
    private String idNumber;

    @Column(name = "id_md5", nullable = false, length = 32)
    private String idMd5;

    @Column(name = "phone", length = 128)
    private String phone;

    @Column(name = "job", length = 128)
    private String job;

    @Column(name = "workplace")
    private String workplace;

    @Column(name = "native_place")
    private String nativePlace;

    @Column(name = "address")
    private String address;

    @Column(name = "remark")
    private String remark;
}
